package com.wdb007.venus.svc.biz.manager;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.wdb007.venus.svc.biz.utils.WsStrUtils;
/**
 * 给书柜发送ws消息
 * @author lenovo
 *
 */

@Component("wsClientSenderBookShelf")
public class WSClientSenderBookShelf implements InitializingBean{
	
	private Logger logger = LoggerFactory.getLogger(WSClientSenderBookShelf.class);
	/**书柜编号*/
	private String bookshelf_code ;
	/**服务器名称*/
	private String serverName = "serverName" ;
	
	private WSBookShelfEnum action ;
	
	private String bookgrid_code = "";
	
	private String gridtransid="";	
	
	private String location = "";
    
	private Long groupId = 0L;
    @Resource(name="sslClient")
    private SSLClient sslClient;
    
    @Autowired
    private ConfigInfoManager configInfoManager;
    
    private URI uri = null ;
    public WSClientSenderBookShelf() {
		
    }
    
    public void wsHeat() {
	    	try {
	    		this.sslClient.send("HeartBeat");
	    	}catch(Exception e) {
	    		logger.error("发送心跳信号异常;进行重连", e);
	    		this.sslClient.reset(uri);
	    	}
    }
    
    
    public void send(String grid_code, String gridtransid, WSBookShelfEnum action, Long groupId) {
    	this.action = action ;
    	this.gridtransid = gridtransid;
		this.bookgrid_code = grid_code;
		this.bookshelf_code = grid_code.substring(0, 10) ;
		this.groupId = groupId;
		send();
    	
    }

	public void send(){
		try {
			//开柜门
			Map<String, String> map = new HashMap<String, String>();
			map.put("bookCaseId", this.bookshelf_code);
			map.put("action", this.action.name());
			if(WSBookShelfEnum.bookgrid_unlock_req.equals(action)
				|| WSBookShelfEnum.bookgrid_check_req.equals(action)
				|| WSBookShelfEnum.bookgrid_status_req.equals(action)) {
				map.put("bookGridId", this.bookgrid_code);
				if(WSBookShelfEnum.bookgrid_unlock_req.equals(action)) {
					map.put("transactionId", this.gridtransid);
				}else {
					map.put("pushId", this.groupId+"");
				}
			}else if(WSBookShelfEnum.bookcase_check_req.equals(action)
				|| WSBookShelfEnum.bookcase_status_req.equals(action)) {
			}
			String msg = JSON.toJSONString(map);
			logger.info("给书柜[{}]发送数据[{}]=========", bookshelf_code, msg);
			this.sslClient.send(msg);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
	}
	
	public static void main(String[] args) {
		
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		location = configInfoManager.getValue("SEND_SHELF_LOCATION") + "&token=" + WsStrUtils.getServerToken(serverName) + "&server=" + serverName;
		try {
			uri = new URI(location);
		} catch (URISyntaxException e) {
			logger.error(e.getMessage());
		}
		String wssConfigPath = System.getenv("WSS_CONFIG_PATH");
		if(null == wssConfigPath) {
			logger.info("uri: {}", uri.toString());
			sslClient.setURI(uri);
		}
	}
}
